<template>
  <div>
    <el-container class="home-container">
      <!-- 头部 -->
      <el-header>
        <div>
          <!-- <img src="../assets/images/header_logo.png" alt=""> -->
          <h2>电商后台管理系统</h2>
        </div>
        <el-button type="info" @click="logOut">退出</el-button>
      </el-header>
      <!-- 页面主体区域 -->
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="collapse?'64px':'200px'">
          <div class="toggle-button" @click="toggle">|||</div>
          <el-menu router :default-active="this.$router.history.current.path" :collapse-transition="false"
            :collapse="collapse" unique-opened background-color="#313743" text-color="#fff" active-text-color="#00a1d6">
            <el-submenu :index="`/${menu1.path}`" v-for="menu1 in menuList" :key="menu1.id">
              <!-- 一级菜单 -->
              <template slot="title">
                <i :class="iconsList[menu1.id]"></i>
                <span style="margin-left:15px">{{menu1.authName}}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item :index="`/${menu2.path}`" v-for="menu2 in menu1.children" :key="menu2.id"><i
                  class="el-icon-menu"></i>
                <span>{{menu2.authName}}</span></el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 右侧内容区 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>

  </div>
</template>

<script>
  export default {
    data() {
      return {
        menuList: [], //左侧列表数据
        iconsList: {
          125: "iconfont icon-user",
          103: "iconfont icon-tijikongjian",
          101: "iconfont icon-shangpin",
          102: "iconfont icon-danju",
          145: "iconfont icon-baobiao",
        }, //字体图标对象
        collapse: false, //控制侧边栏的折叠隐藏
      }
    },
    methods: {
      //退出登录
      logOut() {
        localStorage.clear()
        this.$router.push("/login")
      },
      //获取列表数据
      async getMenuList() {
        let result = await this.$requests.get("menus")
        if (result.meta.status == 200) {
          this.menuList = result.data
        } else {
          console.log("请求失败")
        }
      },
      //折叠展开侧边栏
      toggle() {
        this.collapse = !this.collapse
      }
    },
    mounted() {
      this.getMenuList()
    }
  }

</script>

<style lang="less">
  .home-container {
    height: 100vh;
  }

  .el-header {
    background-color: #363d40;
    display: flex;
    justify-content: space-between;
    align-items: center;

    h2 {
      color: aliceblue;
    }
  }

  .el-aside {
    background-color: #313743;
    transition: 0.2s;

    .el-menu {
      border-right: none;
    }
  }

  .el-main {
    background-color: #eaedf1;
  }

  .toggle-button {
    background-color: #495065;
    font-size: 10px;
    display: flex;
    justify-content: center;
    justify-items: center;
    letter-spacing: 3px;
    cursor: pointer;
  }

</style>
